//index.js
//获取应用实例
const app = getApp()

Page({
  mixins: [require('../mixin/themeChanged')],
  /**
   * 页面的初始数据
   */

  data: {

    weightu:[{name:"会议预约",image:"../images/meeting.png",fun:"gomeeting"}],
    weighta:[{name:"会议预约",image:"../images/meeting.png",fun:"gomeeting"}],
 

    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,

    list:[],

    city: [],

    cityindex: "0",
    userInfo: {
      name: "",
      uid: "",
      company: "",
      department: "",
      city: "",
      status: "",
      avatarUrl: "", //用户头像
      nickName: "", //用户昵称
    },

  },


  bindCityChange: function (e) {
    var that = this
    var url = wx.getStorageSync("config")
    var uid = wx.getStorageSync("uid")
    this.setData({
      cityindex: e.detail.value
    })
    console.log(that.data.city[that.data.cityindex])
    console.log(uid)

    wx.showModal({
      title: '修改确认',
      content: '请确认修改城市',
      success: function (res) {
        if (res.confirm) {


          //请求体
          wx.request({
            url: url.service.homeUrl + '/member/changecity',
            data: {
              uid: uid,
              city: that.data.city[that.data.cityindex],
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: (res) => {

              wx.showToast({
                title: res.data.msg,
              });
              that.login()

            },
            fail: function (res) {
              wx.showToast({
                title: res.data.msg,
              })
            },
            complete: function (res) {
              wx.showToast({
                title: res.data.msg,
              })
            },
          })
        }
      }
    })

  },

  login: function () {
    var that = this
    wx.getUserInfo({
      success: function (res) {
        console.log(res);
        var avatarUrl = 'userInfo.avatarUrl';
        var nickName = 'userInfo.nickName';
        that.setData({
          [avatarUrl]: res.userInfo.avatarUrl,
          [nickName]: res.userInfo.nickName,
        })
      }
    })


    wx.login({
      success: resp => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(resp);
        var that = this;
        var url = wx.getStorageSync("config");
        // 获取用户信息
        wx.getSetting({
          success: res => {
            if (res.authSetting['scope.userInfo']) {
              // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
              wx.getUserInfo({
                success: userResult => {
                  var platUserInfoMap = {}
                  platUserInfoMap["encryptedData"] = userResult.encryptedData;
                  platUserInfoMap["iv"] = userResult.iv;
                  wx.request({
                    url: url.service.homeUrl + '/member/wxlogin',
                    data: {
                      platCode: resp.code,
                      platUserInfoMap: platUserInfoMap,

                    },
                    header: {
                      "Content-Type": "application/json"
                    },
                    method: 'POST',
                    dataType: 'json',
                    success: function (res) {
                      if (res.data.code === 200) {
                        console.log(res)
                        wx.setStorageSync("name", res.data.name) //设置本地缓存
                        wx.setStorageSync("uid", res.data.uid) //设置本地缓存
                        wx.setStorageSync("company", res.data.company) //设置本地缓存
                        wx.setStorageSync("department", res.data.department) //设置本地缓存
                        wx.setStorageSync("city", res.data.city) //设置本地缓存
                        wx.setStorageSync("status", res.data.status)
                        wx.setStorageSync("openid", res.data.openid)  //设置本地缓存
                        wx.showToast({
                          title: res.data.msg,
                          icon: "none"
                        })
                        var name = 'userInfo.name';
                        var company = 'userInfo.company';
                        var department = 'userInfo.department';
                        var city = 'userInfo.city';
                        var status = 'userInfo.status';
                        var uid = 'userInfo.uid';

                        that.setData({
                          [name]: wx.getStorageSync('name'),
                          [company]: wx.getStorageSync('company'),
                          [department]: wx.getStorageSync('department'),
                          [city]: wx.getStorageSync('city'),
                          [status]: wx.getStorageSync('status'),
                          [uid]: wx.getStorageSync('uid'),
                          weightu:res.data.weightu,
                          weighta:res.data.weighta
                        })
                      } else if (res.data.code === 400) {
                        wx.showToast({
                            title: res.data.msg,
                            icon: "none"
                          }),
                          wx.setStorageSync("openid", res.data.openid) //设置本地缓存
                        console.log(wx.getStorageSync('openid'))

                        wx.navigateTo({
                          url: '../reg/reg',
                        })
                      } else {
                        wx.showToast({
                          title: res.data.msg,
                          icon: "none"
                        })
                      }
                    },
                    fail: function (err) {}, //请求失败
                    complete: function () {} //请求完成后执行的函数
                  })
                }
              })
            }
          }
        })
      }
    })
  },


  changeTheme: function() {
    console.log(this.data);
    getApp().themeChanged(this.data.theme === 'light' ? 'dark' : 'light');
},


  gomeeting: function () {
    wx.navigateTo({
      url: "../meeting/meetingorder",
    })
  },

  gothing: function () {
    wx.navigateTo({
      url: "../thing/thingorder",
    })
  },

  goseal: function () {
    wx.navigateTo({
      url: "../seal/sealorder",
    })
  },

  gosealadmin: function () {
    wx.navigateTo({
      url: "../seal/sealadmin",
    })
  },

  gothingadmin: function () {
    wx.navigateTo({
      url: "../thing/thingadmin",
    })
  },

  gothingstorage: function () {
    wx.navigateTo({
      url: "../thing/thingstorage",
    })
  },

  gothinginput:function(){
    wx.navigateTo({
      url: "../thing/thinginput",
    })

  },

  gomail:function(){
    wx.navigateTo({
      url: "../mail/mailorder",
    })

  },

  gomailadmin:function(){
    wx.navigateTo({
      url: "../mail/mailadmin",
    })

  },

  gomeetingadmin:function(){
    wx.navigateTo({
      url: "../meeting/meetingadmin",
    })

  },

  gocard:function(){
    wx.navigateTo({
      url: "../card/cardorder",
    })

  },

  goeating:function(){
    wx.navigateTo({
      url: "../eating/eating",
    })

  },

  gocardadmin:function(){
    wx.navigateTo({
      url: "../card/cardadmin",
    })

  },

  goit:function(){
    wx.navigateTo({
      url: "../it/it",
    })

  },



  





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this
    var url = wx.getStorageSync("config")
    wx.request({
      url: url.service.homeUrl + '/member/getcity',
      data: {},
      // header: {
      //   Authorization: userInfo ? "token " + userInfo.token : ""
      // },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
        for (var i = 0; i < res.data.length; i++) {
          var newarray = [res.data[i].city, ];
          that.data.city = that.data.city.concat(newarray);
          that.setData({
            city: that.data.city
          });
        }
      },
      fail: function (res) {},
      complete: function (res) {},
    })

    wx.request({
      url: url.service.homeUrl + '/document/getimg',
      data: {},
      // header: {
      //   Authorization: userInfo ? "token " + userInfo.token : ""
      // },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: (res) => {
  
          that.setData({
            list: res.data
          })
        console.log(that.data.list)
      },
      fail: function (res) {},
      complete: function (res) {},
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */


onShareAppMessage: function (res) {
  if (res.from === 'button') {
    // 来自页面内转发按钮
    console.log(res.target)
  }
  return {
    title: '自定义转发标题',
    path: '/page/user?id=123'
  }
}
})
